Skip to content

[fix][elixir] faulty free-form object type spec#21113

Merged
wing328 merged 2 commits intoOpenAPITools:masterfrom
efcasado:fix-opts-type-spec
Apr 23, 2025
Merged

[fix][elixir] faulty free-form object type spec#21113
wing328 merged 2 commits intoOpenAPITools:masterfrom
efcasado:fix-opts-type-spec

Conversation

@efcasado
Copy link
Copy Markdown
Contributor

@efcasado efcasado commented Apr 21, 2025

Description

An attempt to fix the issue reported in #21112.

Given the minimal example below

openapi: 3.0.0
info:
  version: 1.0.0
  title: A minimal OpenAPI example
paths:
  /foo:
    post:
      requestBody:
        content:
          application/json:
            schema:
              type: object
        required: true
      responses:
        "200":
          description: Successful operation
        "400":
          description: Invalid operation

version 7.12.0 generates the following code

# NOTE: This file is auto generated by OpenAPI Generator 7.12.0 (https://openapi-generator.tech).
# Do not edit this file manually.

defmodule AMinimalOpenAPIExample.Api.Default do
  @moduledoc """
  API calls for all endpoints tagged `Default`.
  """

  alias AMinimalOpenAPIExample.Connection
  import AMinimalOpenAPIExample.RequestBuilder

  @doc """

  ### Parameters

  - `connection` (AMinimalOpenAPIExample.Connection): Connection to server
  - `body` (map()): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, nil}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec foo_post(Tesla.Env.client, %{optional(String.t) => }, keyword()) :: {:ok, nil} | {:error, Tesla.Env.t}
  def foo_post(connection, body, _opts \\ []) do
    request =
      %{}
      |> method(:post)
      |> url("/foo")
      |> add_param(:body, :body, body)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, false},
      {400, false}
    ])
  end
end

after applying the suggested fix, the generated code looks as follows

# NOTE: This file is auto generated by OpenAPI Generator 7.13.0-SNAPSHOT (https://openapi-generator.tech).
# Do not edit this file manually.

defmodule AMinimalOpenAPIExample.Api.Default do
  @moduledoc """
  API calls for all endpoints tagged `Default`.
  """

  alias AMinimalOpenAPIExample.Connection
  import AMinimalOpenAPIExample.RequestBuilder

  @doc """

  ### Parameters

  - `connection` (AMinimalOpenAPIExample.Connection): Connection to server
  - `body` (%{optional(String.t) => any()}): 
  - `opts` (keyword): Optional parameters

  ### Returns

  - `{:ok, nil}` on success
  - `{:error, Tesla.Env.t}` on failure
  """
  @spec foo_post(Tesla.Env.client, %{optional(String.t) => any()}, keyword()) :: {:ok, nil} | {:error, Tesla.Env.t}
  def foo_post(connection, body, _opts \\ []) do
    request =
      %{}
      |> method(:post)
      |> url("/foo")
      |> add_param(:body, :body, body)
      |> Enum.into([])

    connection
    |> Connection.request(request)
    |> evaluate_response([
      {200, false},
      {400, false}
    ])
  end
end

Note that the inconsistency between the generated types in the @doc and @SPEC attributes has also been addressed.

Again, looking forward to hearing your thoughts @mrmstn! 🙏

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@efcasado efcasado changed the title [fix][elixir] incomplete type spec for untyped objects [fix][elixir] faulty free-form object type spec Apr 21, 2025
@efcasado efcasado force-pushed the fix-opts-type-spec branch from 8a48073 to b14a79e Compare April 21, 2025 20:16
@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 22, 2025

@efcasado thanks for the PR

what about adding test schemas to modules/openapi-generator/src/test/resources/3_0/elixir/petstore-with-fake-endpoints-models-for-testing.yaml to test any type and free form objects?

@efcasado efcasado force-pushed the fix-opts-type-spec branch 3 times, most recently from fbc40e4 to 6bb3d63 Compare April 22, 2025 10:43
@efcasado efcasado force-pushed the fix-opts-type-spec branch from 6bb3d63 to 6d0077e Compare April 22, 2025 11:08
@efcasado
Copy link
Copy Markdown
Contributor Author

@wing328, thanks a lot for your input!

I have just pushed a couple of test schemas to the test OpenAPI spec. I see there was an explicit free-form object already, so I only added an example of an implicit free-form object (ie. additionalProperties is not explicitly set).

@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 22, 2025

lgtm. will get it merged before the upcoming v7.13.0 release due this week

thanks again for the contribution.

@wing328 wing328 merged commit 3233eff into OpenAPITools:master Apr 23, 2025
15 checks passed
@wing328
Copy link
Copy Markdown
Member

wing328 commented Apr 23, 2025

when you've time, can you please pm me via Slack?

https://join.slack.com/t/openapi-generator/shared_invite/zt-2wmkn4s8g-n19PJ99Y6Vei74WMUIehQA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants